package com.web_bloks.project.pages.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.web_bloks.content.client.ViewPage;
import com.web_bloks.document.client.Document;
import com.web_bloks.document.client.DocumentManager;
import com.web_bloks.document.client.DocumentListener;
import com.web_bloks.document.client.NamespaceException;
import com.web_bloks.document.client.Title;
import com.web_bloks.document.client.TitleException;
import com.web_bloks.system.client.Logger;
import com.web_bloks.user.client.StatusMessageServer;
import com.web_bloks.user.client.User;
import com.web_bloks.user.client.UserManager;
import com.web_bloks.user.client.UserMessageServer;
import com.web_bloks.widget.client.EditPageWidget;
import com.web_bloks.widget.client.ViewSourceWidget;
import com.web_bloks.types.client.StateType;

/**
 * Request to display a page
 * - use method 'setRequestedPageParams' to specify input parameters
 * - prepare the 'pagename' for the client: this is useful for the Window title
 * - 
 *  
 * @author Jean-Lou Dupont
 *
 */
public class MainPage extends Composite
	implements ViewPage, DocumentListener {

	PageMessages MSG = GWT.create(PageMessages.class);

	VerticalPanel verticalPanel = null;
	HTML htmlContent = null;
	
	//Document related
	String currentPage = null;
	Document currentDocument = null;
	DocumentManager documentManager = null;
	
	//Page request related
	String requestPageName = null;
	String requestQuery    = null;
	
	//User feedback related
	UserMessageServer   umsg = null;
	StatusMessageServer smsg = null;
	
	//Page tools
	EditPageWidget editPageWidget;
	ViewSourceWidget viewSourceWidget;
	
	public MainPage() {
		super();

		
		documentManager = GWT.create(DocumentManager.class);
		documentManager.addDocumentChangedListener( this );
		documentManager.setAPI("/page/");
		
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setSpacing(2);
		setSize("100%", "100%");
		
		verticalPanel.setSize("100%", "100%");

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setSize("100%", "100%");
		verticalPanel.setCellHeight(horizontalPanel, "100%");
		verticalPanel.setCellWidth(horizontalPanel, "100%");

		htmlContent = new HTML("");
		horizontalPanel.add(htmlContent);
		horizontalPanel.setCellHeight(htmlContent, "100%");
		horizontalPanel.setCellWidth(htmlContent, "100%");
		htmlContent.setSize("100%", "100%");

		final FlexTable flexTableTools = new FlexTable();
		horizontalPanel.add(flexTableTools);
		flexTableTools.setCellSpacing(2);
		flexTableTools.setCellPadding(2);

		editPageWidget = new EditPageWidget();
		flexTableTools.setWidget(1, 0, editPageWidget);
		editPageWidget.setVisible(false);
		flexTableTools.getCellFormatter().setVerticalAlignment(1, 0, HasVerticalAlignment.ALIGN_TOP);
		flexTableTools.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER);
		editPageWidget.setHeight("35");
		flexTableTools.getCellFormatter().setHeight(1, 0, "35");

		editPageWidget.setBaseApiUrl( "#Edit?title={{page}}" );
		
		viewSourceWidget = new ViewSourceWidget();
		flexTableTools.setWidget(0, 0, viewSourceWidget);
		viewSourceWidget.setVisible(false);
		flexTableTools.getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		flexTableTools.getCellFormatter().setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
		viewSourceWidget.setHeight("35");
		flexTableTools.getCellFormatter().setHeight(0, 0, "35");

		viewSourceWidget.setBaseApiUrl( "#{{page}}" );
	}
	/*****************************************
	 * ViewPage INTERFACE 
	 *****************************************/
	public Widget getViewWidget() {
		return this;
	}
	
	public String getViewName() {
		return "Main";
	}
	/*
	 * TODO 
	 * @see com.web_bloks.content.client.ViewPage#getPageName()
	 */
	public String getPageName() {
		return requestPageName;
	}
	/**
	 * @see ViewPage
	 */
	public void setViewParams(String key, String value) {
	}
	/**
	 * @see ViewPage
	 */
	public void setRequestedPageParams(String requestedPage, String query) {
		requestPageName = requestedPage;
		requestQuery    = query;
	}
	/**
	 * Invoke DocumentManager for doing the heavy lifting
	 */
	public void doView() {
		documentManager.load( requestPageName, "main" );
	}
	
	public void setUserMessageServer(UserMessageServer umsg) {
		this.umsg = umsg;
	}
	public void setStatusMessageServer(StatusMessageServer smsg) {
		this.smsg = smsg;
	}

	/****************************************************
	 * DocumentListener interface 
	 ***************************************************/
	/**
	 * Event to signal that 'documentName' is being loaded
	 *  
	 * @param documentName
	 * @param targetRegion : an identifier relating the document and its target rendering/displaying place
	 */
	public void onDocumentChanging(String documentName, String targetRegion) {
	}
	
	/**
	 * 
	 * @param doc the document loaded
	 * @return boolean True if the document was rendered/displayed successfully
	 */
	public boolean onDocumentChanged(Document doc) {
		currentDocument = doc;
		
		if (null==currentDocument) {
			Logger.logInfo(getClass()+".onDocumentChanged: received NULL document");
			smsg.setStatus(StateType.ERROR);
			umsg.sendMessage( MSG.not_found() );
			return false;
		}
		
		if (currentDocument.getState() == Document.STATE_INVALID ) {
			smsg.setStatus(StateType.ERROR);
			umsg.sendMessage( MSG.not_found() );
			return false;
		}
	
		// OK
		htmlContent.setHTML( currentDocument.getPageContent() );
		smsg.setStatus(StateType.OK);
		
		updateTools( );
		
		//rendered
		return true;
	}
	
	/**
	 * Announces the successful rendering/displaying of the document
	 * 
	 * @param doc
	 */
	public void onDocumentRendered(Document doc) {
	}

	/*****************************************
	 * 
	 *****************************************/
	protected void updateTools() {
		
		if ( null == currentDocument ) {
			viewSourceWidget.setVisible( false );
			editPageWidget.setVisible( false );		
			return;
		}
		
		Title title = currentDocument.getPageTitle();
		
		UserManager um = GWT.create( UserManager.class );
		User user = um.getCurrentUser();
		
		viewSourceWidget.setVisible( user.isLogin() );
		editPageWidget.setVisible( user.isAdmin() );
		
		Title rawTitle = null;
		try {
			 rawTitle = title.getRawTitle();
		} catch (TitleException e) {
		} catch (NamespaceException e) {
		}
		
		if (null!=rawTitle) {
			editPageWidget.setPageName( rawTitle.toString() );			
			viewSourceWidget.setPageName( rawTitle.toString() );
		} else {
			Logger.logWarn(getClass()+".updateTools: EXCEPTION raised whilst generating raw title");
			viewSourceWidget.setVisible( false );
			editPageWidget.setVisible( false );
		}
		
	}//
	
	public void doUpdate() {

		updateTools();
	}
	
}//END